﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Entities.Codes;
using Pg.BioMedics.SDR.Data;

namespace Pg.BioMedics.SDR.Services.Impl
{
    /// <summary>
    /// ISecurityService implmentation.
    /// </summary>
    internal class SecurityService : ISecurityService
    {
        #region ISecurityService Members

        /// <summary>
        /// Provides logic for person credentials and status validation
        /// </summary>
        /// <param name="login">Person login</param>
        /// <param name="password">Person password</param>
        /// <returns>true - if user credentials are valid and status is active</returns>
        public bool ValidatePerson(string login, string password)
        {
            try
            {
                using (SDRDataContext dataContext = new SDRDataContext(false))
                {
                    if (!dataContext.PersonManager.ValidateCredentials(login, password))
                        return false;

                    Person person = dataContext.PersonManager.GetPersonByLogin(login);
                    return (person.StatusCode == PersonStatusCodes.Active);
                }
            }
            catch (Exception ex)
            {
                throw new ServiceException("Cannot validate person {0}", ex, login);
            }
        }

        /// <summary>
        /// Provides logic for person status check
        /// </summary>
        /// <param name="login">Person login</param>
        /// <returns>Returns true if person exists and status is active</returns>
        public bool ValidatePerson(string login)
        {
            try
            {
                using (SDRDataContext dataContext = new SDRDataContext(false))
                {
                    Person person = dataContext.PersonManager.GetPersonByLogin(login);
                    if (person == null)
                        return false;
                    else
                        return (person.StatusCode == PersonStatusCodes.Active);
                }
            }
            catch (Exception ex)
            {
                throw new ServiceException("Cannot validate person {0}", ex, login);
            }
        }

        /// <summary>
        /// Provides logic for roles extraction for person given by his / her login
        /// </summary>
        /// <param name="login">Person login</param>
        /// <returns>List of roles or null if person does not exist</returns>
        public string[] GetRolesForPerson(string login)
        {
            try
            {
                using (SDRDataContext dataContext = new SDRDataContext(false))
                {
                    Person person = dataContext.PersonManager.GetPersonByLogin(login);
                    if (person == null)
                        return null;

                    return (from playedRole in dataContext.PersonManager.GetPlayedRoles(person, PlayedRoleStatusCodes.Active)
                            where (playedRole.StartDate <= DateTime.Now) && (playedRole.EndDate >= DateTime.Now)
                            select playedRole.RoleCode).Distinct()
                                                       .ToArray();
                }
            }
            catch (Exception ex)
            {
                throw new ServiceException("Cannot extract roles for person {0}", ex, login);
            }
        }

        #endregion
    }
}
